/*
 * @Author: 郝奔 1425466221@qq.com
 * @Date: 2024-11-29 10:20:27
 * @LastEditors: 郝奔 1425466221@qq.com
 * @LastEditTime: 2024-12-26 13:26:09
 * @FilePath: /HG-dqbasic_open_platform/src/framework/api/user/index.ts
 * @Description: 
 * Copyright (c) 2024-present HBIS Digital Technology Co.,Ltd. All rights reserved. 
 */
import { default as request, cloud } from '@/framework/utils/request';
import { UserType, UserForm, UserInfo, UserQuery, SliderKaptchaInfo, SendMailCodeResponse, SendMailCodeReq, AuthRules, PwdType, LoginParmas, TenantListType, AuthSendMailCodeType } from './types';
import { LoginData, TokenResult } from '@/framework/api/auth/types'
import { ResultData, ResultDataList } from '../common';
import { SmsLoginData } from '@/framework/api/user/types'
import { USERSURL, APPLICATIONJSON } from '@/framework/constants/index.js'
/**
 * 获取滑块验证码
 */
export function getSliderKaptcha(): Promise<ResultData<SliderKaptchaInfo>> {
  return request({
    url: '/auth/kaptcha/getSliderKaptcha',
    method: 'get'
  });
}
/**
 * 登录成功后获取用户信息（昵称、头像、权限集合和角色集合）
 */
export function getUserInfo(token: string): Promise<ResultData<UserInfo>> {
  return request({
    url: cloud.businessBasic + '/auth/getCurrentLoginUserInfo',
    method: 'get',
    params: { token }
  });
}

/**
 * 获取用户分页列表
 *
 * @param queryParams
 */
export function listUserPages(
  queryParams: UserQuery
): Promise<ResultDataList<UserType[]>> {
  return request({
    url: '/api/v1/users/pages',
    method: 'get',
    params: queryParams
  });
}

/**
 * 获取用户表单详情
 *
 * @param userId
 */
export function getUserForm(userId: number): Promise<ResultData<UserForm>> {
  return request({
    url: USERSURL + userId + '/form',
    method: 'get'
  });
}

/**
 * 修改用户
 *
 * @param id
 * @param data
 */
export function updateUser(id: number, data: UserForm): Promise<ResultData<null>> {
  return request({
    url: USERSURL + id,
    method: 'put',
    data: data
  });
}

/**
 * 修改用户状态
 *
 * @param id
 * @param status
 */
export function updateUserStatus(id: number, status: number): Promise<ResultData<null>> {
  return request({
    url: USERSURL + id + '/status',
    method: 'patch',
    params: { status: status }
  });
}

/**
 * 修改用户密码
 *
 * @param id
 * @param password
 */
export function updateUserPassword(id: number, password: string): Promise<ResultData<null>> {
  return request({
    url: USERSURL + id + '/password',
    method: 'patch',
    params: { password: password }
  });
}

/**
 * 删除用户
 *
 * @param ids
 */
export function deleteUsers(ids: string): Promise<ResultData<null>> {
  return request({
    url: USERSURL + ids,
    method: 'delete'
  });
}

/**
 * 下载用户导入模板
 *
 * @returns
 */
export function downloadTemplate(): Promise<ResultData<null>> {
  return request({
    url: '/api/v1/users/template',
    method: 'get',
    responseType: 'arraybuffer'
  });
}

/**
 * 导出用户
 *
 * @param queryParams
 * @returns
 */
export function exportUser(queryParams: UserQuery): Promise<ResultData<null>> {
  return request({
    url: '/api/v1/users/_export',
    method: 'get',
    params: queryParams,
    responseType: 'arraybuffer'
  });
}

/**
 * 导入用户
 *
 * @param file
 */
export function importUser(deptId: number, roleIds: string, file: File): Promise<ResultData<null>> {
  const formData = new FormData();
  formData.append('file', file);
  formData.append('deptId', deptId.toString());
  formData.append('roleIds', roleIds);
  return request({
    url: '/api/v1/users/_import',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  });
}

// 获取租户选择先拉框的接口
export function getTenantDropDownList(): Promise<ResultData<TenantListType[]>> {
  return request({
    url: '/basics/sysTenant/getTenantDropDownList',
    method: 'get'
  })
}
// 滑块成功后点击下一步调用 发送验证码接口
export function sendMailCode(params: SendMailCodeReq): Promise<ResultData<SendMailCodeResponse>> {
  return request({
    url: '/sysUser/sendMailCode',
    method: 'POST',
    data: params
  })
}
// 找回密码-邮箱验证码校验 验证码重置密码合一接口
export function validatorEmailAuthCode(params: string): Promise<ResultData<null>> {
  return request({
    url: '/sysUser/validatorEmailAuthCode',
    method: 'POST',
    data: params,
    headers: {
      'Content-Type': APPLICATIONJSON
    }
  })
}

export function checkPwd(params: string): Promise<ResultData<string>> {
  return request({
    url: '/checkPwd',
    method: 'post',
    data: params,
    headers: {
      'Content-Type': APPLICATIONJSON
    }
  })
}
// 短信验证码登录
export function loginBySms(data: string): Promise<ResultData<string>> {
  return request({
    url: `/loginBySms`,
    method: 'post',
    data,
    headers: {
      'Content-Type': APPLICATIONJSON
    }
  })
}

// 认证策略版本账号密码登录
export function loginApiPolicy(data: LoginData | string): Promise<ResultData<string>> {
  return request({
    url: '/loginApiV2_3',
    method: 'post',
    data,
    headers: {
      'Content-Type': APPLICATIONJSON
    }
  })
}

// 发送验证码\
export function authSendMailCode(data: Partial<SmsLoginData>): Promise<ResultData<string & AuthSendMailCodeType>> {
  return request({
    url: '/sendMailCode',
    method: 'post',
    data,
    headers: {
      'Content-Type': APPLICATIONJSON
    }
  })
}
// 单点短信登录
export function loginSms(data: LoginParmas): Promise<ResultData<TokenResult>> {
  return request({
    url: `/sso/loginSms`,
    method: 'post',
    data
  })
}

// 获取密码策略
export function getPwdConfigByTenantId(params: AuthRules): Promise<ResultData<PwdType>> {
  return request({
    url: '/pwdConfig/getPwdConfigByTenantId',
    method: 'get',
    params
  })
}
// 修改密码
export function expiredResetPassword(data: string): Promise<ResultData<null>> {
  return request({
    url: '/sysUser/expiredResetPassword',
    method: 'POST',
    data,
    headers: {
      'Content-Type': APPLICATIONJSON
    }
  })
}